package com.dyb.recuitment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyb.recuitment.entity.Admin;
import com.dyb.recuitment.entity.Setting;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.mapper.LoginMapper;
import com.dyb.recuitment.mapper.RegisterMapper;
import com.dyb.recuitment.mapper.SettingMapper;
import com.dyb.recuitment.service.ILoginService;
import com.dyb.recuitment.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-01-14
 */
@Service
public class RegisterServiceImpl extends ServiceImpl<RegisterMapper, User> implements IRegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public int register(User user) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(password).setCreateTime(localDateTime).setUpdateTime(localDateTime);
        return registerMapper.insert(user);
    }

    @Override
    public int registerAdmin(Admin admin) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        String password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
        admin.setPassword(password).setCreateTime(localDateTime).setUpdateTime(localDateTime);
        return registerMapper.registerAdmin(admin);
    }

}
